package com.sz.controller;

import com.sz.mapper.BankCardMapper;
import com.sz.model.BankCard;
import com.sz.model.Result;
import com.sz.myinterface.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
public class ToolController {

    @Autowired
    private BankCardMapper bankCardMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //获取ip地址
    @RequestMapping("/ip")
    public String getIp(){

        //获取地址
        String getaddress = Address.getaddress();

        return getaddress;
    }

    //天气
    @RequestMapping(value = "/tq",method = RequestMethod.POST)
    public String getWether(@RequestBody String city){

        String weather1 = stringRedisTemplate.opsForValue().get("weather");

        String weather=null;
        if(weather1!=null&&weather1!=""){
            weather=weather1;
        }else{
            //获取天气信息
            weather = Weather.getWeather(city);
            stringRedisTemplate.opsForValue().set("weather",weather,1,TimeUnit.DAYS);
        }

        return weather;

    }

    //获取银行卡信息--所属银行
    @RequestMapping(value = "/account",method = RequestMethod.POST)
    public Result getBankCard(@RequestBody String card){
        Result result=null;
        System.out.println("对方银行卡号card = " + card);
        //调用银行卡接口查询
        //String card1 = MyBankCard.getBankCard(card);
        String card1 = "其他银行";

        String substring = card.substring(0, 6);

        if(substring.equals("202107")){
            result=new Result(200,"银行卡信息获取成功","深圳知领银行");
        }else{
            result=new Result(200,"银行卡信息获取成功",card1);
        }
        return result;
    }

    //发送验证码
    @RequestMapping(value = "/phone")
    public Result getPhone(String phone){

        Result result=null;
        String s = SMSCode.sendSMS(phone);

        if(s!=null&&s!=""){
            stringRedisTemplate.opsForValue().set(phone,s,60, TimeUnit.SECONDS);

            result=new Result(200,"成功");
        }else{
            result=new Result(301,"错误");
        }

        return result;
    }

    //短信--判断验证码是否正确redis获取
    @RequestMapping(value = "/smscode")
    public Result getPhoneSMS(String phone,String code){
        Result result=null;

        String s = stringRedisTemplate.opsForValue().get(phone);

        if(s!=null&&s!=""){
            if(s.equals(code)){
                result=new Result(200,"正确");
            }else{
                result=new Result(301,"验证码错误");

            }
        }else{
            result=new Result(302,"验证码已过期");
        }

        return result;
    }

    //判断付款账户余额
    @RequestMapping(value = "/amount",method = RequestMethod.POST)//余额，账户银行卡号
    public Result getBankCardAmount(@RequestParam String amount, @RequestParam String account){
        Result result=null;

        //获取余额
        Long aLong = bankCardMapper.selectMoney(account);
        int money=Integer.parseInt(amount);
        if(aLong!=null&&aLong>money){
            result =new Result(200,"余额充足");
        }else{
            result =new Result(301,"余额不足");
        }
        return result;
    }

    @RequestMapping(value = "/state",method = RequestMethod.POST)
    public Result getBankState(@RequestBody String bankAccount) {
        System.out.println("bankAccount = " + bankAccount);
        Result result=null;

        String sixstr=bankAccount.substring(0,6);
        if(sixstr.equals("202107")){
            BankCard bankCard = bankCardMapper.selectMoney2(bankAccount);
            if(bankAccount!=null) {
                result = new Result(200, "银行卡状态", bankCard.getBranchState());
            }else{
                result = new Result(301, "异常");
            }
        }else{
            result = new Result(200, "银行卡状态", "1");
        }

        return result;
    }


}
